package com.nolon.flange.tools

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

object TimeFormatUtil {
    fun getInterval(createAt: Date): String? {
        // 定义最终返回的结果字符串。
        var interval: String? = null
        val millisecond = Date().time - createAt.time
        var second = millisecond / 1000
        if (second <= 0) {
            second = 0
        }
        //*--------------微博体（标准）
        interval = if (second == 0L) {
            "刚刚"
        } else if (second < 30) {
            second.toString() + "秒以前"
        } else if (second in 30..59) {
            "半分钟前"
        } else if (second >= 60 && second < 60 * 60) { //大于1分钟 小于1小时
            val minute = second / 60
            minute.toString() + "分钟前"
        } else if (second >= 60 * 60 && second < 60 * 60 * 24) { //大于1小时 小于24小时
            val hour = second / 60 / 60
            if (hour <= 3) {
                hour.toString() + "小时前"
            } else {
                "今天" + getFormatTime(createAt, "HH:mm")
            }
        } else if (second >= 60 * 60 * 24 && second <= 60 * 60 * 24 * 2) { //大于1D 小于2D
            "昨天" + getFormatTime(createAt, "HH:mm")
        } else if (second >= 60 * 60 * 24 * 2 && second <= 60 * 60 * 24 * 7) { //大于2D小时 小于 7天
            val day = second / 60 / 60 / 24
            day.toString() + "天前"
        } else if (second <= 60 * 60 * 24 * 365 && second >= 60 * 60 * 24 * 7) { //大于7天小于365天
            getFormatTime(createAt, "MM-dd HH:mm")
        } else if (second >= 60 * 60 * 24 * 365) { //大于365天
            getFormatTime(createAt, "yyyy-MM-dd HH:mm")
        } else {
            "0"
        }
        return interval
    }

    fun getFormatTime(date: Date?, Sdf: String?): String {
        return SimpleDateFormat(Sdf, Locale.getDefault()).format(date)
    }

    @JvmStatic
    fun main(args: Array<String>) {
        try {
            println(getInterval(SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-08-03 12:12:12")))
        } catch (e: ParseException) {
            e.printStackTrace()
        }
    }
}